package cn.scw.project.goods.service.impl;

import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.project.facility.service.IFacFacilityService;
import cn.scw.project.goods.domain.GAdvertising;
import cn.scw.project.goods.domain.netework.bean.GAdvertisingAddReq;
import cn.scw.project.goods.mapper.GAdvertisingMapper;
import cn.scw.project.goods.service.IGAdvertisingService;
import cn.scw.project.system.domain.SysDealer;
import cn.scw.project.system.domain.SysUser;
import cn.scw.project.system.service.ISysDealerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 广告Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class GAdvertisingServiceImpl implements IGAdvertisingService {
    @Autowired
    private GAdvertisingMapper gAdvertisingMapper;
    @Autowired
    private ISysDealerService sysDealerService;
    @Autowired
    private IFacFacilityService iFacFacilityService;

    /**
     * 查询广告
     *
     * @param id 广告ID
     * @return 广告
     */
    @Override
    public GAdvertising selectGAdvertisingById(Long id) {
        return gAdvertisingMapper.selectGAdvertisingById(id);
    }

    /**
     * 查询广告列表
     *
     * @return 广告
     */
    @Override
    public List<GAdvertising> selectGAdvertisingList(String facilityCode) {
//        SysUser user=SecurityUtils.getLoginUser().getUser();
//        SysDealer dealer=sysDealerService.selectSysDealerByUserId(user.getUserId());
//        if (dealer!=null)
//            return gAdvertisingMapper.selectGAdvertisingListByDealerId(facilityCode,dealer.getId());
        return gAdvertisingMapper.selectGAdvertisingList(facilityCode);
    }

    /**
     * 新增广告
     *
     * @param req 广告
     * @return 结果
     */
    @Override
    public int insertGAdvertising(GAdvertisingAddReq req) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysDealer dealer= sysDealerService.selectSysDealerByUserId(user.getUserId());
        if (dealer!=null&&!dealer.getJurisAdvertising())
            throw new CustomException("未开启广告权限");
        if (StringUtils.isNotBlank(req.getFacilityCode())) {
            if (!iFacFacilityService.existsByCode(req.getFacilityCode()))
                throw new CustomException("设备不存在");
        }
        if (StringUtils.isBlank(req.getUrl()))
            throw new CustomException("请上传广告图");

        GAdvertising gAdvertising = new GAdvertising();

        ToolUtils.copyPropertiesIgnoreNull(req, gAdvertising);
        if (dealer!=null)
            gAdvertising.setDealerId(dealer.getId());

        return gAdvertisingMapper.insertGAdvertising(gAdvertising);
    }

    /**
     * 修改广告
     *
     * @param req 广告
     * @return 结果
     */
    @Override
    public int updateGAdvertising(GAdvertisingAddReq req) {
        GAdvertising gAdvertising = this.selectGAdvertisingById(req.getId());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        SysDealer dealer= sysDealerService.selectSysDealerByUserId(user.getUserId());
        if (dealer!=null&&!dealer.getJurisAdvertising())
            throw new CustomException("未开启广告权限");

        ToolUtils.copyPropertiesIgnoreNull(req, gAdvertising);
        if (StringUtils.isNotBlank(gAdvertising.getFacilityCode())) {
            if (!iFacFacilityService.existsByCode(req.getFacilityCode()))
                throw new CustomException("设备不存在");
        }

        return gAdvertisingMapper.updateGAdvertising(gAdvertising);
    }

    /**
     * 批量删除广告
     *
     * @param ids 需要删除的广告ID
     * @return 结果
     */
    @Override
    public int deleteGAdvertisingByIds(Long[] ids) {
        return gAdvertisingMapper.deleteGAdvertisingByIds(ids);
    }

    /**
     * 删除广告信息
     *
     * @param id 广告ID
     * @return 结果
     */
    @Override
    public int deleteGAdvertisingById(Long id) {
        return gAdvertisingMapper.deleteGAdvertisingById(id);
    }
}
